package com.atguigu.web;

import com.atguigu.pojo.Food;
import com.atguigu.service.FoodService;
import com.atguigu.service.impl.FoodServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class FoodServlet extends BaseServlet {
    private FoodService foodService = new FoodServiceImpl();

    /**
     * 增加菜品信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        // 1、获取请求的参数==封装成为food对象
        Food food = WebUtils.EncodingChange(req.getParameterMap());

        // 2、调用foodService.addFood()保存菜品
        foodService.addFood(food);
        // 3、跳到菜品列表页面    跳回到列表页面的时候，注意先跳到查询界面，是界面数据刷新
        // 这里不能采用请求转发，原因就是当使用请求转发的时候，用户在按下F5的时候，浏览器又会发起最后一次请求也就是添加菜品，导致重复添加
        resp.sendRedirect(req.getContextPath() + "/manager/foodServlet?action=listLimit&id=1");
    }

    /**
     * 更新菜品信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        // 1、获取请求参数id，菜品编号
        int id = WebUtils.parseInt(req.getParameter("id"));
        // 2、调用foodService.deleteFoodById() 删除菜品
        foodService.deleteFoodById(id);
        // 3、重定向返回菜品列表页面
        resp.sendRedirect(req.getContextPath() + "/manager/foodServlet?action=listLimit&id=1");
    }

    /**
     * 更新菜品信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        // 1、获取请求参数id，菜品编号, 封装成food对象
        Food food = WebUtils.EncodingChange(req.getParameterMap());
        food.setId(WebUtils.parseInt(req.getParameter("id")));
        // 2、调用foodService.updateFood(food) 修改菜品信息
        foodService.updateFood(food);
        // 3、重定向返回菜品列表页面
        resp.sendRedirect(req.getContextPath() + "/manager/foodServlet?action=listLimit&id=1");
    }

    /**
     * 查询所有菜品信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        List<Food> foods = foodService.queryFoods();
        req.setAttribute("msg", foods);
        req.getRequestDispatcher("/pages/manager/food_manager.jsp").forward(req, resp);
    }

    /**
     * 获取修改的菜品信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getFood(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 1、获取菜品编号 id
        String id = req.getParameter("id");
        // 2、查询编号对应的food对象
        Food food = foodService.queryFoodById(WebUtils.parseInt(id));
        // 3、将food对象封装到request域中
        req.setAttribute("foods", food);
        // 4、请求重定向到修改菜品界面
        req.getRequestDispatcher("/pages/manager/food_edit.jsp").forward(req, resp);
    }

    /**
     * 查询分页后的菜品信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void listLimit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        // 1、调用Service层中的查询分页的菜品信息   通过传入的id判断是第几页
        List<Food> foods = foodService.queryLimitFoods(WebUtils.parseInt(id), 4);
        long count = foodService.getCount();
        // 2、将得到的信息写入request域中
        req.setAttribute("msg", foods);
        if (count % 4 == 0) {
            req.setAttribute("pages", count / 4);
        } else {
            req.setAttribute("pages", count / 4 + 1);
        }
        req.setAttribute("number", count);
        // 3、请求转发到  /pages/manager/food_manager.jsp
        req.getRequestDispatcher("/pages/manager/food_manager.jsp").forward(req, resp);
    }

    /**
     * 查询菜类、分页后的菜品信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void listLimitClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 1、调用Service层中的查询分页的菜品信息   通过传入的id判断是第几页
        List<Food> foods = foodService.queryLimitClassFoods
                (WebUtils.parseInt(req.getParameter("food_id")), WebUtils.parseInt(req.getParameter("id")), 4);
        long count = foodService.getClassCount(Integer.valueOf(req.getParameter("food_id")));
        // 2、将得到的信息写入request域中
        req.setAttribute("msg", foods);
        if (count % 4 == 0) {
            req.setAttribute("pages", count / 4);
        } else {
            req.setAttribute("pages", count / 4 + 1);
        }
        req.setAttribute("number", count);
        // 3、请求转发到  /pages/manager/food_manager.jsp
        req.getRequestDispatcher("/pages/manager/food_class_manager.jsp").forward(req, resp);
    }

}
